package com.atguigu.gmall.item.service.impl;

import com.atguigu.gmall.item.service.ItemService;
import com.atguigu.gmall.model.product.BaseCategoryView;
import com.atguigu.gmall.model.product.SkuImage;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.model.product.SpuSaleAttr;
import com.atguigu.gmall.product.feign.ProductFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * projectName: gmall
 *
 * @author: 陈鹏
 * time: 2022/9/25 16:30 星期日
 * description: 商品详情页的Service实现类
 */
@Service
public class ItemServiceImpl implements ItemService {

    @Autowired
    private ProductFeign productFeign;

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;
    /**
     * 查询商品详情页的全部信息
     *
     * @param skuId
     * @return
     */
    @Override
    public Map<String, Object> getItemInfo(Long skuId) {
        //返回结果初始化
        Map<String, Object> result = new HashMap<>();
        //参数校验
        if(skuId == null){
            return result;
        }

        CompletableFuture<SkuInfo> future1 =
                CompletableFuture.supplyAsync(()->{
                    //必须要先查skuInfo的信息
                    SkuInfo skuInfo = productFeign.getSkuInfo(skuId);
                    //判断商品是否存在,若不存在直接结束
                    if(skuInfo == null || skuInfo.getId() == null){
                        return null;
                    }
                    result.put("skuInfo",skuInfo);
                    //返回
                    return skuInfo;
                },threadPoolExecutor);

        CompletableFuture<Void> future2 =
                future1.thenAcceptAsync((skuInfo -> {
                    //判断
                    if(skuInfo==null || skuInfo.getId() == null){
                        return;
                    }
                    //查询分类的信息
                    BaseCategoryView category = productFeign.getCategory(skuInfo.getCategory3Id());
                    result.put("category",category);
                }),threadPoolExecutor);

        CompletableFuture<Void> future3 =
                future1.thenAcceptAsync(skuInfo -> {
                    //判断
                    if(skuInfo==null || skuInfo.getId() == null){
                        return;
                    }
                    //图片的列表
                    List<SkuImage> imageList = productFeign.getImageList(skuInfo.getId());
                    result.put("imageList",imageList);
                },threadPoolExecutor);

        CompletableFuture<Void> future4 =
                future1.thenAcceptAsync(skuInfo -> {
                    //判断
                    if(skuInfo==null || skuInfo.getId() == null){
                        return;
                    }
                    //查询价格
                    BigDecimal price = productFeign.getPrice(skuInfo.getId());
                    result.put("price", price);
                },threadPoolExecutor);

        CompletableFuture<Void> future5 =
                future1.thenAcceptAsync(skuInfo -> {
                    //判断
                    if(skuInfo==null || skuInfo.getId() == null){
                        return;
                    }
                    //销售属性信息,标识出当前sku的销售属性值是哪几个
                    List<SpuSaleAttr> spuSaleAttrList =
                            productFeign.getSpuSaleAttr(skuInfo.getId(),skuInfo.getSpuId());
                    result.put("spuSaleAttrList",spuSaleAttrList);
                },threadPoolExecutor);

        CompletableFuture<Void> future6 =
                future1.thenAcceptAsync(skuInfo -> {
                    //判断
                    if(skuInfo==null || skuInfo.getId() == null){
                        return;
                    }
                    //查询页面跳转使用的键值对
                    Map skuSaleAttrInfo = productFeign.getSkuSaleAttrInfo(skuInfo.getSpuId());
                    result.put("skuSaleAttrInfo",skuSaleAttrInfo);
                },threadPoolExecutor);
        //等待全部线程完成任务
        CompletableFuture.allOf(future1,future2,future3,future4,future5,future6).join();
        //返回结果
        return result;
    }
}
